<?php

class MemeryShare {
	var $_emptyResult = '$$I am empty$$';
	var $cmdids = array();
	var $cmds = array();
	// id,state
	var $STATE = array(
		'new' => 0,
		'ready' => 1,
		'finished' => 2,
		'poped' => 3
	);
	
	function MemeryShare($conf) {
	}
	
	function addTask($cmd, $params, $callback) {
	}
	
	function finishOneTask($id, $result) {
	}
	
	function getNewTasks() {
	}
	
	function getRemainTasks() {
	}
	
	function getTaskResults() {
	}
	
	function cleanTasks($ids) {
	}
	
	function parseResult($result, $callback) {
	}
	
	function calOneTask($id, $func) {
	}
	
	function lockOneTask($id) {
	}
	
	function cacheIDGen() {
		return md5(uniqid());
	}
	
	function cacheValGen($array) {
		return serialize($array);
	}
	
	function cacheValUnGen($val) {
		return unserialize($val);
	}
	
	function cacheValArray($array) {
		$rst = array();
		$rst['state'] = isset($rst['state']) ? $this->STATE['new'] : $rst['state'];
		$rst['result'] = isset($rst['result']) ? $this->_emptyResult : $rst['result'];
		$rst['func'] = $rst['func'];
		$rst['params'] = $rst['params'];
		$rst['callback'] = $rst['callback'];
		return $rst;
	}
}

class MemeryShareMysql {
	
	function MemeryShareMysql($conf) {
		$con = mysql_connect('127.0.0.1', 'root', '');
		$db = mysql_selectdb('proc', $con);
	}
	
	function addTask($cmd, $params, $callback) {
	}
	
	function finishOneTask($id, $result) {
	}
	
	function getNewTasks() {
	}
	
	function getRemainTasks() {
	}
	
	function getTaskResults() {
	}
	
	function cleanTasks($ids) {
	}
	
	function parseResult($result, $callback) {
	}
	
	function calOneTask($id, $func) {
	}
	
	function lockOneTask($id) {
	}
}

class MemeryShareMemcache extends MemeryShare {
	var $cache = NULL;
	//这里面会涉及大量的锁问题
	
	function MemeryShareMemcache($conf) {
	}
	
	function addTask($func, $params, $callback) {
		$id = $this->cacheIDGen();
		array_push($this->cmdids, $id);
		$val = $this->cacheValGen($this->cacheValArray(array(
			'func' => $func,
			'params' => $params,
			'callback' => $callback
		)));
		if (memcache_add($this->cache, $id, $val)) {
			return $id;
		}
	}
	///有handler调用，其他都可以有主进程调用
	
	function finishOneTask($id, $result) {
		$val = memcache_get($this->cache, $id);
		if ($val) {
			$val = $this->cacheValUnGen($val);
		}
		$val['result'] = $result;
		$val = $this->cacheValGen($val);
		if (memcache_set($this->cache, $id, $val)) {
			return TRUE;
		}
		return FALSE;
	}
	
	function getNewTasks() {
		//
		
		return new stdClass();
	}
	
	function getRemainTasks() {
	}
	
	function getTaskResults() {
	}
	
	function cleanTasks($ids) {
	}
	
	function parseResult($result, $callback) {
	}
	
	function calOneTask($id, $func) {
	}
	
	function lockOneTask($id) {
	}
}

class ProcGen {
	var $procHelper = NULL;
	var $sleepTime = 1000;
	
	function ProcGen(MemeryShare $MemerShareClass, $config, $sleepTime = 1000) {
		$this->procHelper = new $MemerShareClass($config);
		$this->sleepTime = $sleepTime;
	}
	///启动
	
	function run() {
		if ($this->procHelper) {
			$task = $this->procHelper->getNewTasks();
			while (TRUE) {
				//$task = $
				if (!$task) {
					$results = $this->procHelper->getTaskResults();
					$remainTasks = $this->procHelper->getRemainTasks();
					real_sleep($this->sleepTime);
				}
				if (!is_object($task) && empty($results) && empty($remainTasks)) break;
				if ($task) {
					$this->procHelper->lockOneTask($task->id);
					//os fixed
					$cmd = 'e:\easyphp\php\php.exe ' . 'proc_handler.php ' . $task->id . " " . $task->func . " " . $task->params . ' ' . session_id();
					pclose(popen("start /B " . $cmd, "r"));
				}
				if (!empty($results)) {
					$this->procHelper->parseResult($results);
				}
			}
		}
	}
}

function real_sleep($mSec) {
	if (!function_exists('socket_create')) {
		die("Please enable extension php_sockets.dll");
	}
	static $socket = FALSE;
	if ($socket === FALSE) {
		$socket = array(
			socket_create(AF_INET, SOCK_RAW, 0)
		);
	}
	$pSock = $socket;
	$uSex = $mSec * 1000;
	$read = $write = array();
	$tSex = 0;
	socket_select($read, $write, $pSock, $tSex, $uSex);
	return TRUE;
}
?>